<template>
	<view class="goods-list">
		<view class="top" v-if="!condition.activityId">
			<view class="search-warpper">
				<view class="search" @click="toSearch">
					<view class="left">
						<u-icon name="search" color="#999" size="36"></u-icon>
					</view>
					<view class="keyword">
						{{condition.keyword}}
					</view>
				</view>
			</view>
			<view class="condition">
				<view :class="['item',condition.sortField == item.value ? 'act-item':'']" v-for="(item,index) in conditionList" :key="index" @click="select(item,index)">
					<view>{{item.label}}</view>
					<view class="right" v-if="index==3">
						<u-icon name="arrow-up-fill" :color="condition.sortField == item.value&&condition.sortOrder=='asc'?'#0D95FF':'#666'" size="12"></u-icon>
						<u-icon name="arrow-down-fill" :color="condition.sortField == item.value&&condition.sortOrder=='desc'?'#0D95FF':'#666'" size="12"></u-icon>
					</view>
				</view>
			</view>
		</view>
		<view :class="['content',!condition.activityId?'u-m-t-168':'']">
			<view v-for="(item,index) in goodsSearchList.data" :key="index">
				<goods-card :goods="item"></goods-card>
			</view>
			<u-empty v-if="goodsSearchList.data.length==0 && loadingStatus=='nomore'" mode="list" text="数据为空" margin-top="400"></u-empty>
			<view class="load-more" v-if="goodsSearchList.data.length>0 || loadingStatus!=='nomore'">
				<u-loadmore :status="loadingStatus" />
			</view>
		</view>
		<cart />
	</view>
</template>

<script>
	import { mapActions, mapState } from 'vuex';
	import { strHighlight } from "@/common/utils/tools.js"
	import goodsCard from "@/components/goods/card.vue"
	import cart from "@/components/cart/cart.vue"
	export default{
		components:{ goodsCard, cart },
		props:{},
		data(){
			return {
				condition:{
					storeId:uni.getStorageSync('storeId'),
					keyword:'',
					sortField:'',
					sortOrder:'',
					size:10,
					activityId:''
				},
				options:{},
				conditionList:[{
					label:'综合排序',
					value:''
				},{
					label:'销量优先',
					value:'salenum'
				},{
					label:'有优惠',
					value:'isActivity'
				},{
					label:'价格',
					value:'goodsPrice'
				}]
			}
		},
		onLoad(options) {
			this.options = options
			this.condition.keyword = options.keyword || ''
			this.condition.activityId = options.activityId || ''
			this.getGoodsSearchList(1)
			uni.setNavigationBarTitle({
				　　title:options.activityName
				})
		},
		onReachBottom() {
			console.log(111)
			if (this.loadingStatus === 'loadmore') {
				this.getGoodsSearchList();
			}
		},
		computed:{
			...mapState({
				loadingStatus: state => state.goods.loadingStatus,
				goodsSearchList: state => state.goods.goodsSearchList,
			})
		},
		methods:{
			...mapActions(['ActGetGoodsSearchList']),
			async getGoodsSearchList(current){
				await this.ActGetGoodsSearchList({
					...this.condition,
					current:current || this.goodsSearchList.current + 1
				})
				this.set()
			},
			toSearch(){
				this.$u.route({
					url:"pages/goods/goodsSearch",
					params:{
						keyword:this.options.keyword
					}
				})
			},
			select(item,index){
				console.log(this.condition.sortOrder,index)
				this.condition.sortField = item.value
				if(index==3){
					this.condition.sortOrder = !this.condition.sortOrder ? 'asc' : this.condition.sortOrder === 'desc'?'asc':'desc'
				}else{
					this.condition.sortOrder = ''
				}
			},
			// 设置
			set(){
				let value = this.options.keyword;
				this.goodsSearchList.data.map((item,index)=>{
					item.goodsName = strHighlight(item.goodsName,value)
					item.brandName = strHighlight(item.brandName,value)
				})
			},
		},
		watch:{
			'condition.sortField'(sortField){
				if(sortField !== 'goodsPrice'){
					this.getGoodsSearchList(1)
				}
			},
			'condition.sortOrder'(){
				if(this.condition.sortField === 'goodsPrice'){
					this.getGoodsSearchList(1)
				}
			},
		}
	}
</script>

<style lang="scss" scoped>
	.goods-list{
		padding-bottom: 100rpx;
		.top{
			position: fixed;
			width: 100%;
			/* #ifdef H5 */
			top: 88rpx;
			/* #endif */
			/* #ifdef MP-WEIXIN */
			top: 0;
			/* #endif */
			z-index: 1;
		}
		.search-warpper{
			background-color: #fff;
			padding: 12rpx 24rpx;
			.search{
				background-color: $u-bg-color;
				border-radius: 250rpx;
				height: 64rpx;
				padding: 10rpx 22rpx;
				color: red;
				.left{
					float: left;
				}
				.keyword{
					font-size: 32rpx;
					color: $u-main-color;
					background-color: #fff;
					padding: 0 16rpx;
					line-height: 44rpx;
					border-radius: 50rpx;
					float: left;
					margin-left: 10rpx;
				}
			}
		}
		.condition{
			background-color: #fff;
			height: 80rpx;
			display: flex;
			align-items: center;
			justify-content: space-around;
			font-size: 24rpx;
			.item{
				display: flex;
				align-items: center;
				.right{
					display: flex;
					flex-direction: column;
					align-items: center;
					margin-left: 10rpx;
				}
			}
			.act-item{
				color: $u-type-primary;
			}
		}
		.content{
			// margin-top: 168rpx;
		}
		.u-m-t-168{
			margin-top: 168rpx;
		}
		.load-more{
			padding: 20rpx 0;
		}
	}
</style>
